<template>
	<view class="pagesLogin">
		<view class="loginContent" v-if="isShow">
			<u--form ref="form" :model="form" label-position="left" :labelWidth="80"
				:labelStyle="{color:'#3D3D3D',fontSize:'28rpx',fontWeight:'bold'}">

				<u-form-item label="绑定手机号" prop="phone">
					<u--input type="number" placeholder="请输入绑定手机号" v-model="form.phone"></u--input>
				</u-form-item>
				<view class="btn">
					<u-button type="primary" @click="submitForm">确认绑定</u-button>
				</view>
			</u--form>
		</view>




	</view>
</template>

<script>
	import {
		getEmpower,
		getBinding
	} from '@/api/weixin/weixin.js'
	import {
		setToken,
	} from '@/utils/auth'
	const filter = require('@/utils/filter.js');

	export default {
		data() {
			return {
				params: {
					// appid: 'wx10d9329f9276b891', // 公众号 APP ID
					appid: 'wx606f33c38576bd9e', // 公众号 APP ID
					// redirect_uri: `${encodeURI('http://8815o5733g.goho.co/pages/login/login')}`, // 授权后重定向的回调链接地址， 请使用 urlEncode 对链接进行处理
					redirect_uri: `${encodeURI('http://h5.shanyijiankang.com/pages/login/login')}`, // 授权后重定向的回调链接地址， 请使用 urlEncode 对链接进行处理
					response_type: 'code', // 固定写法
					scope: 'snsapi_userinfo' // snsapi_base 静默授权获取 open id ；snsapi_userinfo 需要用户授权，获取详细信息
					// state:'code', // a-zA-Z0-9的参数值，最多128字节
				},
				code: '', // 微信返回 code
				WX_AUTH_URL: 'https://open.weixin.qq.com/connect/oauth2/authorize?',
				REDIRECT: '#wechat_redirect',
				isShow: false,
				form: {
					userId: '',
					phone: ''
				},

			};
		},
		onLoad() {

		},
		onShow() {
			//#ifdef H5
			if (this.$wx.isWechat()) { //微信环境才走授权
				// if (filter.getUrlParam("code") == undefined || filter.getUrlParam("code") == null || filter.getUrlParam(
				// 		"code") == "") {
				// 	this.init()

				// } else {
				// 	this.code = filter.getUrlParam("code")
				// 	this.getEmpower(this.code)
				// }
				this.$tab.reLaunch('/pages/loginBefore/loginBefore')
			}else{
				this.$tab.reLaunch('/pages/loginBefore/loginBefore')
			}
			//#endif
		},
		methods: {
			init() {
				// debugger
				// 访问地址
				const access_url = this.WX_AUTH_URL + `${new URLSearchParams(this.params)}` + this.REDIRECT;
				// 获取地址参数
				const params = new URLSearchParams(location.search);
				this.code = params.get('code');
				console.log(this.code)
				if (!this.code) {
					location.href = access_url
				}

			},

			getEmpower() {
				getEmpower({
					code: this.code
				}).then(res => {
					if (res.code == 200) {
						if (res.data.isBinding == '0') {
							this.form.userId = res.data.userId
							this.isShow = true
						} else if (res.data.isBinding == '1') {
							setToken(res.data.token)
							this.$store.commit('SET_TOKEN', res.data.token)
							this.loginSuccess()

						}

					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				// 设置用户信息
				this.$store.dispatch('GetInfo').then(res => {
					this.$tab.reLaunch('/pages/index/index')
				})
			},
			submitForm() {
				if (!this.form.phone) {
					this.$modal.msg("请输入手机号")
				} else {
					this.checkPhone(this.form.phone)
				}
			},
			getBinding() {
				getBinding(this.form).then(res => {
					if (res.code == 200) {
						setToken(res.data)
						this.$store.commit('SET_TOKEN', res.data)
						this.loginSuccess()
					}
				})

			},
			checkPhone(phone) {
				var reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
				console.log('reg.test(phone)', reg.test(phone))
				if (reg.test(phone) == false) {
					this.$modal.msg("手机号码格式不正确")
					return
				} else {
					this.getBinding()
				}
			}
		}

	}
</script>

<style lang="scss" scoped>
	.pagesLogin {

		display: flex;
		justify-content: center;
		align-items: center;
		padding-top: 400rpx;

		.loginContent {
			box-shadow: 0rpx 4rpx 24rpx 0rpx rgba(0, 0, 0, 0.08);
			border-radius: 8rpx;
			margin-bottom: 20rpx;
			padding: 40rpx;

			.btn {
				width: 180rpx;
				margin: 20rpx auto;
			}
		}

	}
</style>